
function checkUser(){
	if(!(document.getElementById('nameList').selectedIndex == 0)) {
		document.getElementById('login').disabled = false;
		document.getElementById('PWD').disabled = false;
		document.getElementById('PWD').value = "";
		document.getElementById('PWD').focus();
	} else{
		document.getElementById('login').disabled = true;
		document.getElementById('PWD').disabled = true;
	}
} // end checkUser

function getNameList() {
	var str = sendGETRequest("../getNames.php");
   // Format der von str: name1,status1;name2,status2;name3,status3
	var nameList = [];
	var group = str.split(";");
	
	// String in 2D Array aufteilen
	for(i in group) { 
		nameList.push(group[i].split(",")) 
	};

	var select = document.getElementById( "nameList" );
	var optgroup = document.createElement( "optgroup" );
	optgroup.label = "Aktivitas";
	select.appendChild( optgroup );
	
	var optgroup2 = document.createElement( "optgroup" );
	optgroup2.label = "Altherrenschaft";
	select.appendChild( optgroup2 );
	
	var optgroup3 = document.createElement( "optgroup" );
	optgroup3.label = "Fax";
	select.appendChild( optgroup3 );
	
	for(i in group) {	
		var option    = new Option();
		option.value = (nameList[i][0]);
		option.appendChild( document.createTextNode(nameList[i][0]));
			
		if(  nameList[i][1] == 'FX'  ||  nameList[i][1] == 'AB'   ||  nameList[i][1] == 'iAB'  ){	
			optgroup.appendChild( option );
		} else if( nameList[i][1]== 'AH' ){
			optgroup2.appendChild( option );
			
	    } else if( nameList[i][1]== 'FAX' ){
			optgroup3.appendChild( option );
		}

	} // end for
	

	document.getElementById('nameList').selectedIndex = 0;
} // end getNameList

function add(vari) {
	document.getElementById(vari).value = (parseInt(document.getElementById(vari).value) + 1);
} // end add

function subt(vari) {
	document.getElementById(vari).value = Math.max(0, (parseInt(document.getElementById(vari).value) - 1) );
} // end subt

function sendGETRequest(request) {
	var xmlhttp = null;
				
	// Mozilla
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.open("GET", request, false);
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			$returnVal  = xmlhttp.responseText;
		}
	}
	
	xmlhttp.send(null);
	
	return $returnVal;
} // end sendGETRequest
	
function abort() {	
	var nrElements = document.getElementsByName('drink');
	
	for(var i = 0; i < nrElements.length; i++) {
		document.getElementsByName('drink')[i].value =0;
	}
	document.getElementById('password').value = "xxxxxxxx";
	document.getElementById('nameList').selectedIndex = 0;
	checkUser();
} // end abort

function pay(){
		var amount = parseFloat( document.getElementById('payment').value);
		var user   = document.getElementById('nameList').value;

		if(amount > 0 && user != "Name wählen..."){
				var myRequest = "updateSaldo.php?user="+user+"&amount=" + amount;	
				var newSaldo    = parseFloat( sendGETRequest(myRequest) ) ;
				
				alert('Geld gutgeschrieben.\n Der neue Kontostand des Nutzers '+ user + ': ' + newSaldo+' Euro');
				
		} else {
				alert('Bitte erst einen Betrag und Namen für die Gutschrift eigeben');
		}
}// end pay


function listBookings(pathAdd){
		var user  = document.getElementById('nameList').value;
		var myRequest = pathAdd+"listBookings.php?name=" + user;
		var bookingList  = sendGETRequest(myRequest) ;
		document.getElementById('bookingList').innerHTML  =  bookingList;
}// end listBookings

function pwCheckAndList(){
	var userName  = document.getElementById('nameList').value;
	var pass = hex_md5( document.getElementById('password').value );
	
	var myRequest = "authUser.php?name=" + userName + "&pw=" + pass;
	var response  = sendGETRequest(myRequest); //userData = {Name;Hash;Rechte;Status}
	
	if (response == 'done') {
		listBookings('');
	} else {
		alert('Anmeldung fehlgeschlagen');
	}	
} // end pwCheckAndList


function addUser(){
	var pass1      =  document.getElementById('newUserPass').value;
	var pass2      =  document.getElementById('newUserPassConfirm').value;
	var state      =  document.getElementById('newUserState').value;
	var rights     =  document.getElementById('newUserRights').value;
	var userName   =  document.getElementById('newUserName').value;
	var mail       =  document.getElementById('newUserMail').value;
	
		
	if(pass1 == pass2){
		var myRequest = "addUser.php?name="+userName+"&mail=" + mail +"&pw=" + pass1+"&state=" + state	+"&rights=" + rights;	
		 sendGETRequest(myRequest);
		 
		 alert(myRequest+'\nNutzer erfolgreich hinzugefügt');
	} else {
		alert('Passwörter stimmen nicht überein');
	}
} // end addUser

function changeAccountSetting(name){
	var pass1   = document.getElementById('password1').value;
	var pass2   = document.getElementById('password2').value;
	var newMail = document.getElementById('mail').value;
	
	if (pass1 == '' && pass2 == ''){
		var myRequest = "updateUser.php?name=" + name + "&mail=" + newMail;	
		sendGETRequest(myRequest);
		
		alert('Änderungen übernommen.');
	} else {
		if(pass1 == pass2) {
			var hash = hex_md5(pass1);
			var myRequest = "updateUser.php?name=" + name + "&mail=" + newMail + "&pw=" + hash;	
			
			sendGETRequest(myRequest);
			
			alert('Änderungen übernommen.');
		} else {
			alert('Passwörter stimmen nicht überein.');
		}
	}
} // changeAccountSetting

function generateInvoices(){
	var sendByMail  = 'false';
	var zipFile     = 'false';
	var pdfFile     = 'false';
	var group       = 'false';
	var user        = 'false';
	var timeFrom;
	var timeTo;
	
	timeFrom = document.getElementById('from').value;
	timeTo   = document.getElementById('to').value;
	
	if ( document.getElementById('processOptions').selectedIndex == 1 ||
		 document.getElementById('processOptions').selectedIndex == 3    ) {
		 
		 sendByMail = 'true';
	}
	
	if ( document.getElementById('processOptions').selectedIndex == 0 ||
		 document.getElementById('processOptions').selectedIndex == 1    ) {
		 
		 pdfFile = 'true';
	} else {
		 zipFile = 'true';
	}
	
	if ( document.getElementsByName('radio')[0].checked) {		 
		 group = 'ALL';
	} else if ( document.getElementsByName('radio')[1].checked) {
		 group = 'ACTIVE';
    } else if ( document.getElementsByName('radio')[2].checked) {
		 group = 'AHAH';
	} else if (document.getElementsByName('radio')[3].checked) {
		 user = document.getElementById('nameList').value;
	}
	
	
	var myRequest = "./invoice/generatePDFInvoices.php?name="+user+"&group=" + group +"&timeFrom=" + timeFrom +"&timeTo=" + timeTo +"&pdf=" + pdfFile+"&zip=" + zipFile+ "&mail=" + sendByMail;	

	window.open(myRequest,'Bierabrechnung','width=400,height=600')
}

function showPWChangeForm(){
	var myRequest = "accountChangeForm.php";
	var form  = sendGETRequest(myRequest) ;
	document.getElementById('adminControl').innerHTML  =  form;
}

function showNewUser(){
	var myRequest = "userMngmtForm.php";
	var form  = sendGETRequest(myRequest) ;
	document.getElementById('adminControl').innerHTML  =  form;
}

function showAccounts(){
	var myRequest = "bookingsForm.php";
	var form  = sendGETRequest(myRequest) ;
	document.getElementById('adminControl').innerHTML  =  form;
	getNameList();
}

function showPayment(){
	var myRequest = "paymentForm.php";
	var form  = sendGETRequest(myRequest) ;
	document.getElementById('adminControl').innerHTML  =  form;
	getNameList();
}

function showInvoice(){
	var myRequest = "invoiceForm.php";
	var form  = sendGETRequest(myRequest) ;
	document.getElementById('adminControl').innerHTML  =  form;
	getNameList();
}

function showStock(){
	var myRequest = "stockForm.php";
	var form  = sendGETRequest(myRequest) ;
	document.getElementById('adminControl').innerHTML  =  form;
	getNameList();
}